package com.quiz.bis.controller;

import com.quiz.bis.domain.bo.ExamSubgroupAddBo;
import com.quiz.bis.domain.bo.ExamSubgroupMemberBo;
import com.quiz.bis.domain.bo.ExamSubgroupUpdateBo;
import com.quiz.bis.service.IExamSubgroupService;
import com.quiz.common.core.controller.BaseController;
import com.quiz.common.core.domain.R;
import com.quiz.common.mybatis.core.page.PageQuery;
import com.quiz.common.mybatis.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分组管理控制器
 * @author ahuan
 */
@RestController
@RequestMapping("/exam/group/subgroup")
@Tag(name = "分组管理")
@RequiredArgsConstructor
public class ExamSubgroupController extends BaseController {
    private final IExamSubgroupService subgroupService;

    @GetMapping("/list/{groupId}")
    @Operation(summary = "获取分组列表")
    public TableDataInfo<?> listSubgroups(@PathVariable Long groupId) {
        return subgroupService.listSubgroups(groupId);
    }

    @PostMapping
    @Operation(summary = "添加分组")
    public R<Long> addSubgroup(@Validated @RequestBody ExamSubgroupAddBo bo) {
        return subgroupService.addSubgroup(bo);
    }

    @PutMapping
    @Operation(summary = "更新分组")
    public R<Void> updateSubgroup(@Validated @RequestBody ExamSubgroupUpdateBo bo) {
        return subgroupService.updateSubgroup(bo);
    }

    @DeleteMapping
    @Operation(summary = "删除分组")
    public R<Void> deleteSubgroups(@RequestParam List<Long> ids) {
        return subgroupService.deleteSubgroups(ids);
    }

    @GetMapping("/members/{subgroupId}")
    @Operation(summary = "获取分组成员列表")
    public TableDataInfo<?> listSubgroupMembers(
            @PathVariable Long subgroupId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String searchKey) {

        PageQuery pageQuery = new PageQuery(pageNum, pageSize);
        return subgroupService.listSubgroupMembers(subgroupId, pageQuery, searchKey);
    }

    @GetMapping("/members2")
    @Operation(summary = "获取分组成员列表")
    public TableDataInfo<?> listSubgroupMembers2(
            @RequestParam Long subgroupId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String searchKey) {

        PageQuery pageQuery = new PageQuery(pageNum, pageSize);
        return subgroupService.listSubgroupMembers(subgroupId, pageQuery, searchKey);
    }

    @PostMapping("/members")
    @Operation(summary = "添加分组成员")
    public R<Void> addSubgroupMembers(@Validated @RequestBody ExamSubgroupMemberBo bo) {
        return subgroupService.addSubgroupMembers(bo);
    }

    @DeleteMapping("/members")
    @Operation(summary = "移除分组成员")
    public R<Void> removeSubgroupMembers(@RequestBody ExamSubgroupMemberBo bo) {
        return subgroupService.removeSubgroupMembers(bo);
    }
}